package com.zhida.jiyangcenter.utils

import android.content.Context
import android.text.InputFilter
import chooongg.base.utils.toast
import java.util.regex.Pattern

object FilterUtil {
    //过滤表情正则表达式
    val EMOJI = "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]"
    //匹配昵称
    val MATCHER_NICKNAME = "[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+"

    //匹配数字、中英文、标点符号
    val MATCHER_DEFAULT = "[A-Za-z0-9\\u4e00-\\u9fa5\\p{P}]+"

    //过滤金额
    val MATCHER_MONEY = "(^[1-9]\\d*(\\.\\d{1,2})?$)|(^[0]{1}(\\.\\d{1,2})?$)"

    //匹配中文字符
    val MATCHER_CN = "[\\u4e00-\\u9fa5]"

    val MATCHER_BQ = "[`~!‼〽@#$%^&*()+=|{}':;',\\[\\].<>/?~@①#￥%……&*（）——+|{}【】‘；：”“’、]"

    /**
     * @param context
     * @return 返回EditText输入文本过滤器
     */
    fun getInputFilter(context: Context, toasts: String, regex: String): InputFilter {
        return InputFilter { charSequence, start, end, dest, dstart, dend ->
            if (charSequence.length > 0) {
                if (isMatcher(charSequence.toString(), regex) && !isMatcher(charSequence.toString(), MATCHER_BQ)) {
                    return@InputFilter null
                } else {
                    toast(toasts)
                    return@InputFilter ""
                }
            }
            null
        }
    }

    /**
     * 正则表达式匹配
     *
     * @param content 待匹配内容
     * @param regex   正则表达式
     * @return true 匹配成功
     */
    fun isMatcher(content: String, regex: String): Boolean {
        val pattern = Pattern.compile(regex)
        val matcher = pattern.matcher(content)
        return matcher.find()
    }
}
